package
{
	import arens.Arena;
	import arens.EnumArens;
	
	import feathers.controls.text.TextFieldTextEditor;
	import feathers.core.PopUpManager;
	import feathers.themes.ABWTheme;
	
	import flash.filesystem.File;
	
	import managers.Config;
	import managers.LoaderArens;
	
	import starling.display.Image;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.AssetManager;
	
	import utils.LoaderResource;
	
	public class RootClass extends Sprite
	{
		//[Embed(source = "../bin-debug/Art/Background.png")]
		//private var _background:Class;
		private var textLoader:TextField;
		private var bg:Quad ;
		private var thems:ABWTheme;
		public var loaderArena:LoaderArens;
		private static var _instance:RootClass;
		public var config:Config;
		
		public var arenaLayer:Sprite = new Sprite();
		public var uiLayer:Sprite = new Sprite();
		
		public static function get instance():RootClass{
			if(!_instance){
				_instance = new RootClass();
			}
			return _instance;
		}
		
		public function RootClass()
		{
			super();
			_instance = this;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		private function init(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			/*var background:Image = new Image(Texture.fromBitmap(new _background));
			addChild(background);
			background.width = stage.stageWidth;
			background.height = stage.stageHeight;*/
			thems = new ABWTheme();
			addChild(arenaLayer);
			addChild(uiLayer);
			PopUpManager.root = uiLayer;
			
			bg = new Quad(stage.stageWidth,stage.stageHeight,0xFFAA00);
			arenaLayer.addChild(bg);
			
			textLoader = new TextField(stage.width, 30,"Loading...");
			textLoader.y= (stage.height - textLoader.height)>>1;
			textLoader.x = ((stage.width - textLoader.width)/2)>>1;
			arenaLayer.addChild(textLoader);
			
			var loader:LoaderResource = LoaderResource.instance;
			var appDir:File = File.applicationDirectory;
			loader.assetManager.enqueue(appDir.resolvePath("../bin-debug/Art/MainFon.png"),appDir.resolvePath("../bin-debug/Art/Loader.png"));
			loader.assetManager.loadQueue(onCompleteLoad);
			
			config = Config.getInsance();
		}
		
		private function onCompleteLoad(ratio:Number):void{
			if(ratio>=1){
			 	var img:Image = new Image(LoaderResource.instance.assetManager.getTexture("MainFon"));
				arenaLayer.addChild(img);
				arenaLayer.removeChild(textLoader);
				arenaLayer.removeChild(bg);
				loaderArena = new LoaderArens(EnumArens.MAIN_SCREEN,arenaLayer);
			}
			else{
				textLoader.text = "Loading ..."+ (ratio*100)+"%";
			}
		}
	}
}